package com.bw1806.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bw1806.mapper.TbGoodsMapper;
import com.bw1806.pojo.TbGoods;
import com.bw1806.pojo.TbGoodsExample;
import com.bw1806.sellergoods.service.TbGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2020/11/1115:02
 */
@Service(timeout = 5000,retries = 0)
public class TbGoodsServiceImpl implements TbGoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Override
    public List<TbGoods> findAll() {
        return tbGoodsMapper.selectByExample(null);
    }

    @Override
    public PageResult<TbGoods> pagelist(TbGoods tbGoods,int pageNum,int pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        TbGoodsExample example=new TbGoodsExample();

        TbGoodsExample.Criteria criteria = example.createCriteria();

        if(tbGoods!=null){
            //if(tbGoods.getName()!=null && tbGoods.getName().length()>0){
            //    criteria.andNameLike("%"+tbGoods.getName()+"%");
           // }
        }

        Page<TbGoods> page= (Page<TbGoods>) tbGoodsMapper.selectByExample(example);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void save(TbGoods tbGoods) {
        Long id = tbGoods.getId();
        if(id != null && id !=0){
            tbGoodsMapper.updateByPrimaryKey(tbGoods);
        } else{
            tbGoodsMapper.insert(tbGoods);
        }
    }

    @Override
    public TbGoods getById(Long id) {
        return tbGoodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deletebth(Long[] ids) {
       for(long id : ids){
            tbGoodsMapper.deleteByPrimaryKey(id);
        }
        //tbGoodsMapper.deletebth(ids);
    }
}
